#!/usr/bin/env python

import pygame, sys, math, random, time
import Numeric

if "-p" in sys.argv:
	import psyco
	psyco.full()


from OpenGL.GL import *

from accel import AccelControl

from vector import *

MAXRADIUS=35.0

current=None



def loadImage(image):
    textureSurface = pygame.image.load(image)
 
    textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
 
    width = textureSurface.get_width()
    height = textureSurface.get_height()
 
    texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, texture)
    #glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    #glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
        GL_UNSIGNED_BYTE, textureData)
 
    return texture, width, height



def input(events):
	global current
	
	for event in events:
		if event.type == pygame.QUIT: 
			sys.exit(0)
		elif event.type == pygame.KEYDOWN:
			sys.exit(0)
		elif event.type == pygame.MOUSEBUTTONDOWN:
			sys.exit(0)
	   



if __name__=="__main__":
	pygame.init()


	flags=0
	if "-d" in sys.argv: flags|=pygame.DOUBLEBUF
	if "-f" in sys.argv: flags|=pygame.FULLSCREEN
	flags|=pygame.HWSURFACE|pygame.OPENGL
	window = pygame.display.set_mode((480, 640), flags)
	
	screen = pygame.display.get_surface()
	
	glViewport( 0, 0,480,640 )
	glMatrixMode( GL_PROJECTION )
	glLoadIdentity( )
	glOrtho(0,480,640,0,-1,1)
	glMatrixMode( GL_MODELVIEW )


	

	print "OpenGL!"

	r=0
	frames=0
	last=start=time.time()
	while True: 
		input(pygame.event.get())

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
		
		glLoadIdentity()
		glTranslatef(240,320,0)
		glRotatef(r, 0,0,1)

		glBegin(GL_QUADS)
		
		glColor3f(1,0,0)
		glVertex3f(-210,-210,0)

		glColor3f(0,1,1)
		glVertex3f(210,-210,0)

		glColor3f(1,1,0)
		glVertex3f(210,210,0)

		glColor3f(1,0,1)
		glVertex3f(-210,210,0)

		
		glEnd();



		pygame.display.flip()

		# dump debug things

		now=time.time()
		r+=30*(now-last)

		last=now
		
		sys.stdout.write("\r% 2.2f"%((frames/(now-start))))
		frames+=1

		sys.stdout.flush()


